package com.ruoyi.user.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.vo.UserInfoVo;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.user.domain.UserInfo;
import com.ruoyi.user.domain.bo.UserFileUpdateBo;
import com.ruoyi.user.domain.bo.UserInfoBo;
import com.ruoyi.user.service.IUserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 人员信息
 *
 * @author strongme
 * @date 2023-04-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/user/user")
public class UserInfoController extends BaseController {

    private final IUserInfoService iUserInfoService;

    /**
     * 查询人员信息列表
     */
    @GetMapping("/list")
    public TableDataInfo<UserInfoVo> list(UserInfoBo bo, PageQuery pageQuery) {
        return iUserInfoService.queryPageList(bo, pageQuery);
    }

    @PostMapping("/query")
    public R<List<UserInfoVo>> listNoPage(@RequestBody UserInfoBo bo) {
        return R.ok(iUserInfoService.queryList(bo));
    }

    /**
     * 导出人员信息列表
     */
    @Log(title = "人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserInfoBo bo, HttpServletResponse response) {
        List<UserInfoVo> list = iUserInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "人员信息", UserInfoVo.class, response);
    }

    /**
     * 获取人员信息详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<UserInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                 @PathVariable Long id) {
        return R.ok(iUserInfoService.queryById(id));
    }

    /**
     * 新增人员信息
     */
    @Log(title = "人员信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<UserInfoBo> add(@Validated(AddGroup.class) @RequestBody UserInfoBo bo) {
        iUserInfoService.insertByBo(bo);
        return R.ok(bo);
    }

    /**
     * 修改人员信息
     */
    @Log(title = "人员信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<UserInfoBo> edit(@Validated(EditGroup.class) @RequestBody UserInfoBo bo) {
        iUserInfoService.updateByBo(bo);
        return R.ok(bo);
    }

    /**
     * 删除人员信息
     *
     * @param ids 主键串
     */
    @Log(title = "人员信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iUserInfoService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 更新社戒社康人员文书信息
     *
     * @param bo
     * @return
     */
    @RepeatSubmit()
    @PostMapping("/updateFileInfo")
    public R<Boolean> updateFileInfo(@RequestBody UserFileUpdateBo bo) {
        iUserInfoService.updateFile(bo);
        return R.ok(Boolean.TRUE);
    }

    /**
     * 更新用户为脱失脱管
     * @param userId
     * @return
     */
    @GetMapping("tstg/{userId}")
    public R<Void> tstgUser(@PathVariable("userId") Long userId) {
        UserInfoVo userInfo = iUserInfoService.queryById(userId);
        if (userInfo == null) {
            return R.fail("人员不存在");
        }
        userInfo.setTstg("Y");
        iUserInfoService.updateUserInfo(userInfo);
        return R.ok();
    }

    /**
     * 更新用户康复场所
     * @param userId
     * @param kfcs
     * @return
     */
    @GetMapping("kfcs/{userId}")
    public R<Void> kfcs(@PathVariable("userId") Long userId, @RequestParam("kfcs")Long kfcs) {
        UserInfoVo userInfo = iUserInfoService.queryById(userId);
        if (userInfo == null) {
            return R.fail("人员不存在");
        }
        userInfo.setKfjzzx(kfcs);
        iUserInfoService.updateUserInfo(userInfo);
        return R.ok();
    }

}
